/**
 * 
 */
package com.effectiv.gooruda.rest.service.impl;

import java.util.List;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.DirectMessage;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.rest.ext.ExtResponse;
import com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService;


/**
 * @author Dhrubo
 *
 */
@Component
public class TwitterDirectMessageServiceImpl extends
		TwitterDirectMessageService {
	
	@Autowired
	private SocialResolver resolver;
	
	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#deleteDirectMessage(java.lang.String, long)
	 */
	@Override
	public Response deleteDirectMessage(String provider, long messageId) {
		Twitter twitter = resolver.resolveTwitter(provider);
		twitter.directMessageOperations().deleteDirectMessage(messageId);
		
		ExtResponse res = new ExtResponse();
		res.setMessage("Direct message deleted.");
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#getDirectMessage(java.lang.String, long)
	 */
	@Override
	public Response getDirectMessage(String provider, long id) {
		Twitter twitter = resolver.resolveTwitter(provider);
		DirectMessage message = twitter.directMessageOperations().getDirectMessage(id);
		
		ExtResponse res = new ExtResponse();
		res.setValue(message);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#getDirectMessagesReceived(java.lang.String, int, int, long, long)
	 */
	@Override
	public Response getDirectMessagesReceived(String provider, int page,
			int pageSize, long sinceId, long maxId) {
		Twitter twitter = resolver.resolveTwitter(provider);
		List<DirectMessage> messages = null;
		
		if(sinceId == -1 || maxId == -1){
			messages = twitter.directMessageOperations().getDirectMessagesReceived(page, pageSize);
		}
		else{
			messages  = twitter.directMessageOperations().getDirectMessagesReceived(page, pageSize, sinceId, maxId);
		}
		
		ExtResponse res = new ExtResponse();
		res.setValue(messages);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#getDirectMessagesSent(java.lang.String, int, int, long, long)
	 */
	@Override
	public Response getDirectMessagesSent(String provider, int page,
			int pageSize, long sinceId, long maxId) {
		Twitter twitter = resolver.resolveTwitter(provider);
		List<DirectMessage> messages = null;
		
		if(sinceId == -1 || maxId == -1){
			messages = twitter.directMessageOperations().getDirectMessagesSent(page, pageSize);
		}
		else{
			messages  = twitter.directMessageOperations().getDirectMessagesSent(page, pageSize, sinceId, maxId);
		}
		
		ExtResponse res = new ExtResponse();
		res.setValue(messages);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#sendDirectMessage(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Response sendDirectMessage(String provider, String toScreenName,
			String text) {
		Twitter twitter = resolver.resolveTwitter(provider);
		DirectMessage message = twitter.directMessageOperations().sendDirectMessage(toScreenName, text);
		ExtResponse res = new ExtResponse();
		res.setValue(message);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

	/* (non-Javadoc)
	 * @see com.effectiv.gooruda.rest.service.api.TwitterDirectMessageService#sendDirectMessage(java.lang.String, long, java.lang.String)
	 */
	@Override
	public Response sendDirectMessage(String provider, long toUserId,
			String text) {
		Twitter twitter = resolver.resolveTwitter(provider);
		DirectMessage message = twitter.directMessageOperations().sendDirectMessage(toUserId, text);
		ExtResponse res = new ExtResponse();
		res.setValue(message);
		
		GenericEntity<ExtResponse> entity = new GenericEntity<ExtResponse>(res, ExtResponse.class);
		
		ResponseBuilder builder = Response.ok(entity);
		
		return builder.build();
	}

}
